package xyz.playedu.api.controller.backend;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.api.utils.DateUtils;
import xyz.playedu.common.types.paginate.DeviceTypePaginateFiler;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.inspection.domain.DeviceTypeDict;
import xyz.playedu.inspection.service.DeviceTypeDictService;

import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/backend/v1/devicetype")
public class DeviceTypeDictController {

    @Autowired
    private DeviceTypeDictService  deviceTypeDictService;


//    @BackendPermission(slug = BPermissionConstant.DEVICE_TYPE_DICT)
    @PostMapping("/create")
    public JsonResponse create(@RequestBody DeviceTypeDict deviceTypeDict) {
        HashMap<String, Object> data = new HashMap<>();
        deviceTypeDict.setCreateTime(DateUtils.getNowDate());
        data.put("data", deviceTypeDictService.save(deviceTypeDict));
        return JsonResponse.data(data);
    }

    @PostMapping("/updateDeviceTypeById")
    public JsonResponse updateDeviceTypeById(@RequestBody DeviceTypeDict deviceTypeDict) {
        HashMap<String, Object> data = new HashMap<>();
        deviceTypeDict.setUpdateTime(DateUtils.getNowDate());
        data.put("data", deviceTypeDictService.updateById(deviceTypeDict));
        return JsonResponse.data(data);
    }

    @GetMapping("/getList")
    public JsonResponse getList(@RequestParam HashMap<String, Object> params){
        Integer page = MapUtils.getInteger(params, "page", 1);
        Integer size = MapUtils.getInteger(params, "size", 10);

        String deviceType = MapUtils.getString(params, "deviceType");
        DeviceTypePaginateFiler filer = new DeviceTypePaginateFiler();
        filer.setDeviceType(deviceType);

        PaginationResult<DeviceTypeDict> result = deviceTypeDictService.paginate(page, size, filer);

        HashMap<String, Object> data = new HashMap<>();
        data.put("data",result.getData());
        data.put("total",result.getTotal());

        return JsonResponse.data(data);
    }

    @GetMapping("/getDeviceTypeById/{id}")
    public JsonResponse getDeviceTypeById(@PathVariable(name="id") Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",deviceTypeDictService.getDeviceTypeById(id));
        return JsonResponse.data(map);
    }

    @GetMapping("/delInfoById/{id}")   public JsonResponse delInfoById(@PathVariable(name="id") Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",deviceTypeDictService.delInfoById(id));
        return JsonResponse.data(map);
    }
}
